// pages/akxiche/akxiche.js
const app = getApp();

import {
  hexMD5
} from "../../utils/md5.js"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    USER_ASSETS_TICKET_NAME: '',
    openid: '',
    USER_ASSETS_TICKET_PHONE: '',
    CONSIGNEE_ADDRESS: '',
    POST_TYPE: '',


    L2name: '',
    L3name: '',
    L4name: '',
    L2code: '',
    L3code: '',
    city: [], //市
    district: [], //区
    business: '', //网点
    shi_k: true,
    qu_k: true,
    wang_k: true,

    // 上个页面数据
    shoujihao: '',
    shenfenyi: '',
    shenfener: '',
    qianzi: '',
    renyi: '',
    rener: '',
    yangshi: '',
    shenfenyier: '',
    shenfenerer: '',
    shenfenyisan: '',
    shenfenersan: '',
  },





  aabb() {
    let that = this


    if (that.data.USER_ASSETS_TICKET_NAME == '' || that.data.USER_ASSETS_TICKET_PHONE == '' || that.data.CONSIGNEE_ADDRESS == '' || that.data.L2name == '' || that.data.L3name == '' || that.data.L4name == '') {
      wx.showToast({
        title: '请完善信息！',
        icon: 'none',
        duration: 2000
      })
    } else {

      wx.request({
        url: app.globalData.interface+'/post/API_1115_getOpenIdType',
        method: 'get',
        data: {
   
          POST_PHONE_WHITE: that.data.shoujihao,
          openid: this.data.openid,
          OPENID: this.data.openid,
          OPEN_ID: this.data.openid,
          session_key: this.data.session_key,
          OPEN_UUID: hexMD5(`${this.data.openid.substring(0,20)}${this.data.session_key.substring(0,10)}`)


        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          console.log(res);
          console.log(res.data);

          that.setData({
            POST_TYPE: res.data.POST_TYPE,
            POST_PHONE_WHITE: res.data.POST_PHONE_WHITE,
            ORDERID: res.data.ORDERID,
          })


          if (res.data.result == 'error') {
            wx.showModal({
              title: '温馨提示',
              content: res.data.data,
              success: function (res) {
                if (res.confirm) { //这里是点击了确定以后
                  console.log('用户点击确定')
                } else { //这里是点击了取消以后
                  console.log('用户点击取消')
                }
              }
            })
          } else {
            that.tijiao()

          }
        }
      })
    }
  },





  tijiao() {
    let that = this
    wx.request({
      url: app.globalData.interface+'/post/API_1113_savePostPersonalization',
      method: 'get',
      data: {
        POST_NAME: that.data.USER_ASSETS_TICKET_NAME,
        POST_NOTE: that.data.CONSIGNEE_ADDRESS,
        POST_PHONE: that.data.USER_ASSETS_TICKET_PHONE,
        L4name: that.data.L4name,
        L3name: that.data.L3name,
        L2name: that.data.L2name,
        OPENID: that.data.openid,
        ORDERID: that.data.ORDERID,
        POST_TYPE: that.data.POST_TYPE,
        POST_PHONE_WHITE: that.data.POST_PHONE_WHITE,
        ID_CARD: that.data.shenfenyi,
        ID_CARD1: that.data.shenfener,

        ID_CARDTWO: that.data.shenfenyier,
        ID_CARDTWO2: that.data.shenfenerer,
        ID_CARDTHREE: that.data.shenfenyisan,
        ID_CARDTHREE2: that.data.shenfenersan,
        SIGNATURE: that.data.qianzi,
        PHOTO: that.data.renyi,
        PHOTO1: that.data.rener,
        DEMO_TYPE: that.data.yangshi,

        openid: this.data.openid,  
        OPEN_ID: this.data.openid,
        session_key: this.data.session_key,
        OPEN_UUID: hexMD5(`${this.data.openid.substring(0,20)}${this.data.session_key.substring(0,10)}`)
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res);
        console.log(res.data);


        if (res.data.result == 'error') {
          wx.showModal({
            title: '温馨提示',
            content: res.data.data,
            success: function (res) {
              if (res.confirm) { //这里是点击了确定以后
                console.log('用户点击确定')
              } else { //这里是点击了取消以后
                console.log('用户点击取消')
              }
            }
          })
        } else {

          console.log(111);
          wx.showModal({
            title: '温馨提示',
            content: res.data.data,
            success: function (res) {
              if (res.confirm) { //这里是点击了确定以后
                console.log('用户点击确定')
                wx.switchTab({
                  url: '../../pages/newshouye/newshouye',
                })
              } else { //这里是点击了取消以后
                console.log('用户点击取消')
                wx.switchTab({
                  url: '../../pages/newshouye/newshouye',
                })
              }
            }
          })

        }
      }
    })


    // }
  },









  //验证手机号
  mobileInput(e) {
    let value = e.detail.value.replace(/\D/g, '')
    console.log(value);
    this.setData({
      mobile: value,
    })

    var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(19[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (this.data.mobile.length == 0) {
      wx.showToast({
        title: '输入的手机号为空，请重新输入！',
        icon: 'none',

        duration: 1500
      })
      this.setData({
        USER_ASSETS_TICKET_PHONE: '',
        mobileFormat: false,
      })
    } else if (this.data.mobile.length < 11) {

      wx.showToast({
        title: '手机号长度有误，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        USER_ASSETS_TICKET_PHONE: '',
        mobileFormat: false,
      })

    } else if (!myreg.test(this.data.mobile)) {
      wx.showToast({
        title: '手机号有误，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        USER_ASSETS_TICKET_PHONE: '',
        mobileFormat: false,
      })

    } else {
      this.setData({
        mobileFormat: true,
      })
    }
  },





  // 地区数据
  region() {
    wx.request({
      url: app.globalData.interface+'/newVersion/API_2029_outletPush',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res.data.data);
        this.setData({
          city: res.data.data,
          openid: this.data.openid,  
          OPEN_ID: this.data.openid,
          session_key: this.data.session_key,
          OPEN_UUID: hexMD5(`${this.data.openid.substring(0,20)}${this.data.session_key.substring(0,10)}`)
        })

      }
    })
  },


  but_shi() {
    this.setData({
      shi_k: false
    })
  },

  but_xian() {

    console.log(111);
    if (this.data.L2code == '') {
      wx.showToast({
        title: '请先选择市',
        icon: 'error',
        duration: 2000
      })
    } else {
      this.setData({
        qu_k: false
      })
    }
  },

  but_w() {

    if (this.data.L3code == '') {
      wx.showToast({
        title: '请先选择区',
        icon: 'error',
        duration: 2000
      })
    } else {
      this.setData({
        wang_k: false
      })
    }

  },


  //市
  buttn_c(e) {
    console.log(111);
    console.log(e);
    console.log(e.currentTarget.dataset.item.district);
    console.log(e.currentTarget.dataset.item.L2code);
    this.setData({
      district: e.currentTarget.dataset.item.district,
      L2code: e.currentTarget.dataset.item.L2code,
      L2name: e.currentTarget.dataset.item.L2name,
      shi_k: true

    })

  },

  buttn_x(e) {
    console.log(e);


    this.setData({
      L3code: e.currentTarget.dataset.item.L3code,
      L3name: e.currentTarget.dataset.item.L3name,
      qu_k: true
    })
    this.Outlets()

  },


  buttn_w(e) {
    console.log(e);
    this.setData({
      L4name: e.currentTarget.dataset.item.L4name,
      EMPLOYEE_ID: e.currentTarget.dataset.item.EMPLOYEE_ID,
      wang_k: true
    })
  },

  //网点
  Outlets() {
    wx.request({
      url: app.globalData.interface+'/newVersion/API_2027_outletList',
      method: 'POST',
      data: {
        L2code: this.data.L2code,
        L3code: this.data.L3code
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res);
        console.log(res.data.data);
        this.setData({
          business: res.data.data,
          openid: this.data.openid,  
          OPEN_ID: this.data.openid,
          session_key: this.data.session_key,
          OPEN_UUID: hexMD5(`${this.data.openid.substring(0,20)}${this.data.session_key.substring(0,10)}`)
        })

      }
    })
  },






  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {
    console.log(JSON.parse(e.data));
    var zongshu = JSON.parse(e.data)
    console.log(zongshu[0]);
    console.log(zongshu[1][0]);
    this.setData({

      session_key: app.globalData.session_key,
      openid: app.globalData.openid,
      shoujihao: zongshu[0],

      shenfenyi: zongshu[3][0],
      shenfener: zongshu[3][1],
      shenfenyier: zongshu[3][2],
      shenfenerer: zongshu[3][3],
      shenfenyisan: zongshu[3][4],
      shenfenersan: zongshu[3][5],


      qianzi: zongshu[2],
      renyi: zongshu[1][0],
      rener: zongshu[1][1],
      yangshi: zongshu[4],
      

    })



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // this.setData({
    //   openid: app.globalData.openid
    // })
    this.region()
    this.region()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})